class Solution {
public: 
    void sortColors(int A[], int n){
        int i = 0;
        int j = n - 1;
        int k = n - 1;

        while ( i <= j){
            if (A[i] == 2){
                int temp = A[k];
                A[k] = A[i];
                A[i] = temp;
                k--;
                if{j > k}
                    j = k;
            }
            else if (A[i] == 1){
                int temp = A[j];
                A[j] = A[i];
                A[i] = temp;
                j--;
            }
            else i++;
        }

    }
};